package com.campus_information_portal.service.impl;

import com.campus_information_portal.dao.AcademyDao;
import com.campus_information_portal.dao.CourseDao;
import com.campus_information_portal.entity.Course;
import com.campus_information_portal.entity.CourseAcademy;
import com.campus_information_portal.service.CourseService;
import com.campus_information_portal.util.Message;
import com.campus_information_portal.util.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 20155878 鄢前州
 * @Description 课程管理
 * @create 22:09 2018/4/27
 **/
@Service
public class CourseImpl implements CourseService {
    @Autowired
    private CourseDao courseDao;
    @Autowired
    private AcademyDao academyDao;

    /**
     * @Description 添加课程
     * @author 20155878 鄢前州
     * @methodName addCourse
     * @returnType boolean
     * @create 21:34 2018/4/29
     **/
    @Override
    public Message addCourse(Course course) {
        CourseAcademy checkCourse = courseDao.queryCouseBycourse_num(course);
        //System.out.println("实现层："+checkCourse);//检查添加的课程是否重复
        if (checkCourse != null) {
            return Message.fail().add("你添加的课程已存在", 0);
        } else {
            boolean flag = courseDao.addCourse(course, course.getCourseId());
            System.out.println(flag);
            if (flag) {
                return Message.success().add("添加课程成功", 1);
            } else {
                return Message.fail().add("添加课程失败", 0);
            }
        }
    }

    /**
     * @Description 按id删除课程
     * @author 20155878 鄢前州
     * @methodName deleteCourseBycourse_id
     * @returnType boolean
     * @create 22:46 2018/4/28
     **/
    @Override
    public Message deleteCourseBycourse_id(Integer courseId) {
        CourseAcademy checkCourse = courseDao.queryCouseBycourse_id(courseId);//判断要删除的课程是否存在
        if (checkCourse != null) {
            boolean flag = courseDao.deleteCourseBycourse_id(courseId);
            if (flag) {
                return Message.success().add("删除课程成功", 1);
            }
            return Message.fail().add("删除课程失败", 0);
        } else {
            return Message.fail().add("删除的课程不存在！", 0);
        }
    }

    /**
     * @Description 写代码删除方便，按课程名称删除课程
     * @author 20155878 鄢前州
     * @methodName deleteCoursecourse_name
     * @returnType boolean
     * @create 23:10 2018/5/12
     **/
    public Message deleteCoursecourse_name(String courseName) {
        boolean flag = courseDao.deleteCoursecourse_name(courseName);
        if (flag) {
            return Message.success();
        }
        return Message.fail();
    }

    /**
     * @param
     * @Description 查找所有课程
     * @author 20155878 鄢前州
     * @methodName queryCouseBycourse_id
     * @returnType Course
     * @create 21:38 2018/4/27
     **/
    @Override
    public Message queryAllCouse() {
        List<Course> list = new ArrayList<Course>();
        list = courseDao.queryAllCouse();
        System.out.println("-courseimpl-queryAllCouse-" + list.size());
        for (Course c : list) {
            System.out.println("-courseimpl-queryAllCouse-type-" + c.getCourseType());
        }
        if (list.size() != 0) {
            return Message.success().add("1", list);
        }
        return Message.fail();
    }

    /**
     * @Description 隐式连课程、老师、学院查询
     * @author 20155878 鄢前州
     * @methodName queryAllCouse2
     * @returnType List
     * @create 22:43 2018/5/6
     **/
    @Override
    public Message queryAllCouse2() {
        List<CourseAcademy> list = courseDao.queryAllCouse2();
        if (list.size() > 0) {
            return Message.success().add("courseAcademy", list);
        }
        return Message.fail();
    }

    /**
     * @Description 按课ID称查找课程
     * @author 20155878 鄢前州
     * @methodName queryCousecourse_name
     * @returnType Course
     * @create 10:27 2018/5/2
     **/
    @Override
    public Message queryCouseBycourse_id(int courseId) {
        CourseAcademy courseN = new CourseAcademy();
        courseN = courseDao.queryCouseBycourse_id(courseId);
        if (courseN != null) {
            return Message.success().add("查询课程成功！", 1);
        }
        return Message.fail();
    }

    /**
     * @Description 按照课程代码查找课程
     * @author 20155878 鄢前州
     * @methodName queryCouseBycourse_num
     * @returnType Course
     * @create 21:49 2018/5/2
     **/
    @Override
    public Msg queryCouseBycourse_num(Course course) {
        CourseAcademy courserN = new CourseAcademy();
        courserN = courseDao.queryCouseBycourse_num(course);
        if (courserN != null) {
            return Msg.addData(100, "成功", courserN);
        }
        return Msg.fail();
    }

    /**
     * @Description 按课程类型查找课程
     * @author 20155878 鄢前州
     * @methodName queryCousecourse_type
     * @returnType list集合
     * @create 21:51 2018/5/2
     **/
    @Override
    //有问题，不知道怎么了，查询不出数据
    public Msg queryCousecourse_type(Course course) {
        List<CourseAcademy> list = courseDao.queryCousecourse_type(course);
        if (list.size() != 0) {
            Msg.addData(100, "通过类型查询课程成功", list);
        }
        return Msg.fail();
    }

    /**
     * @Description 通过学院名称查询学院id
     * @author 20155878 鄢前州
     * @methodName queryacademy_idByacademy_name
     * @returnType int
     * @create 9:46 2018/5/10
     **/
    @Override
    public Integer querycourse_idBycourse_name(String course_name) {
        Integer course_id = courseDao.querycourse_idBycourse_name(course_name);
        return course_id;
    }

    /**
     * @Description 通过课程id更新课程信息
     * @author 20155878 鄢前州
     * @methodName updateCourseBycourse_id
     * @returnType boolean
     * @create 22:11 2018/4/28
     **/
    @Override
    public Message updateCourseBycourse_id( Course course) {
        CourseAcademy checkCouse=courseDao.queryCouseBycourse_id(course.getCourseId());
        if (checkCouse!=null){
            boolean flag= courseDao.updateCourseBycourse_id( course);
            if (flag) {
                return Message.success().add("课程更新成功！",1);
            }
            return Message.fail().add("课程更新失败！",0);
        }else{
            return Message.fail().add("要更新的课程不存在！",0);
        }
    }



    /**
     * @param
     * @Description 不重复课程类型
     * @author 20155884 王晓阳
     * @methodName
     * @returnType
     * @create 2018/5/26 15:56
     **/
    @Override
    public Message queryAllCouseDistict() {
        List<Course> list = new ArrayList<Course>();
        list = courseDao.queryAllCouseDistict();
        System.out.println("-courseimpl-queryAllCouseDistict-" + list.size());
        for (Course c : list) {
            System.out.println("-courseimpl-queryAllCouseDistict-type-" + c.getCourseType());
        }
        if (list.size() != 0) {
            return Message.success().add("1", list);
        }
        return Message.fail();
    }

    /**
     * @param
     * @Description 不重复通过课程课程类型查找课程名称
     * @author 20155884 王晓阳
     * @methodName
     * @returnType
     * @create 2018/5/26 15:56
     **/
    @Override
    public Message queryCousecourseType(Course course) {
        List<Course> courseList = new ArrayList<Course>();
        courseList = courseDao.queryCousecourseType(course);
        System.out.println("-courseimpl-queryCousecourseType-" + courseList.size());
        for (Course c : courseList) {
            System.out.println("-courseimpl-queryCousecourseType-type-" + c.getCourseType());
        }
        if (courseList.size() != 0) {
            return Message.success().add("courseList", courseList);
        }
        return Message.fail();
    }

    /**
     * @param
     * @Description 通过课程名称课程类型查找课程id
     * @author 20155884 王晓阳
     * @methodName
     * @returnType
     * @create 2018/5/27 10:53
     **/
    @Override
    public Msg queryCouseId(String courseType,String courseName){
        Course course = courseDao.queryCouseId(courseType,courseName);
        System.out.println("-courseimpl-queryCouseBycourseTypeAndCourseName-" + course.getCourseId());
        if (course!= null) {
            return Msg.addData(100,"course", course);
        }
        return Msg.fail();
    }
}
